package com.bandit.controller.front;

import com.bandit.pojo.Message;
import com.bandit.pojo.MsgType;
import com.bandit.service.MessageService;
import com.bandit.service.MsgTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/front")
@Slf4j
public class MessageFrontController {

    private static final String prefix = "front";

    @Autowired
    MessageService messageService;

    @Autowired
    MsgTypeService msgTypeService;

    @RequestMapping("/toMsgDetails")
    public String toDetails(@RequestParam("msgID") int msgID, Model model){
        log.info("获取信息的细节");
        Message message = messageService.selectMessageByMsgID(msgID);
        model.addAttribute("messageByID",message);
        MsgType msgType = this.msgTypeService.selectMsgTypeByTypeID(message.getMsgType());
        model.addAttribute("messageType",msgType);
        return prefix+"/details";
    }

    @RequestMapping("/message/list/{msgType}")
    public String messageList(@PathVariable("msgType") int msgType, Model model){
        log.info("按类型查询信息列表");
        MsgType type = msgTypeService.selectMsgTypeByTypeID(msgType);
        List<Message> messageListByType = messageService.selectMessageList(new Message(msgType));
        model.addAttribute("messageList",messageListByType);
        model.addAttribute("type",type);
        return prefix+"/type";
    }
}
